<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 15, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

/**
 * Class that keep all respond infomation
 *
 * @static 
 */
class SysResponse {
	
	/**
	 * Get the response instance of System
	 *
	 * @return SiteResponse
	 */
	static function &getInstance() {
		static $instance;
		if (empty ( $instance )) {
			RCoreImporter::cls( "base.SiteResponse" );
			$instance = new SiteResponse ( );
			$config = SystemSite::getSiteConfig ();
			
			$instance->setPageTitle ( $config->MetaTitle );
			$instance->addMetaData ( "description", $config->MetaDesc );
			//$instance->addMetaData ( "keywords", $config->MetaKeys );
			$instance->addMetaData ( "author", $config->MetaAuthor );
		}
		return $instance;
	}
	
	/**
	 * Add a Javascript File into response
	 *
	 * @param string $urlFile url to the javascript file
	 */
	static function addJavascriptFile($urlFile, $media = null) {
		$inst = &SysResponse::getInstance ();
		$inst->addJavascriptFile ( $urlFile, $media );
	}
	
	/**
	 * Add a Javascript content into response
	 *
	 * @param string $jsContent javascript content
	 */
	static function addJavascriptContent($jsContent, $media = null) {
		$inst = SysResponse::getInstance ();
		$inst->addJavascriptContent ( $jsContent, $media );
	}
	
	/**
	 * Add a StyleSheet File into response
	 *
	 * @param string $urlFile url to the StyleSheet file
	 */
	static function addStyleSheetFile($urlFile, $media = null) {
		$inst = SysResponse::getInstance ();
		$inst->addStyleSheetFile ( $urlFile, $media );
	}
	
	/**
	 * Add a StyleSheet content into response
	 *
	 * @param string $jsContent StyleSheet content
	 */
	static function addStyleSheetContent($styleSheetContent, $media = null) {
		$inst = SysResponse::getInstance ();
		$inst->addStyleSheetContent ( $styleSheetContent, $media );
	}
	
	/**
	 * Add the meta data for response
	 *
	 * @param string $name
	 * @param string $content
	 */
	static function addMetaData($name, $content) {
		$inst = SysResponse::getInstance ();
		$inst->addMetaData ( $name, $content );
	}
	
	/**
	 * Remove a metadata out of render
	 *
	 * @param string $name
	 */
	static function removeMetaData($name) {
		$inst = SysResponse::getInstance ();
		$inst->removeMetaData ( $name );
	}
	
	/**
	 * Append the meta data for response
	 *
	 * @param string $name
	 * @param string $content
	 */
	static function appendMetaData($name, $content) {
		$inst = SysResponse::getInstance ();
		$inst->appendMetaData( $name, $content );
	}
	
	/**
	 * prepend the meta data for response
	 *
	 * @param string $name
	 * @param string $content
	 */
	static function prependMetaData($name, $content) {
		$inst = SysResponse::getInstance ();
		$inst->prependMetaData( $name, $content );
	}
	
	/**
	 * Append a string into content of respond
	 *
	 * @param string $appendedStr
	 */
	static function appendAppContent($appendedStr) {
		$inst = SysResponse::getInstance ();
		$inst->appendAppContent ( $appendedStr );
	}
	
	/**
	 * Add the input string before the current appContent
	 *
	 * @param string $prependString
	 */
	static function prependAppContent($prependString) {
		$inst = SysResponse::getInstance ();
		$inst->prependAppContent ( $prependString );
	}
	
	/**
	 * Set the application content for this respond
	 *
	 * @param string $appContent
	 */
	static function setAppContent($appContent) {
		$inst = SysResponse::getInstance ();
		$inst->setAppContent ( $appContent );
	}
	
	/**
	 * Set the title of respond
	 *
	 * @param string $title
	 */
	static function setPageTitle($title) {
		$inst = SysResponse::getInstance ();
		$inst->setPageTitle ( $title );
	}
	
	/**
	 * Append the current title with the input string.
	 * Ex: Current title: "Framework tile"
	 * 		Input string: " | Hello World"
	 * 	-> new title string: "Framework tile | Hello World"
	 *  
	 * @param string $string
	 */
	static function appendPageTitle($string, $separator = " - ") {
		$inst = SysResponse::getInstance ();
		$inst->appendPageTitle ( $separator . $string );
	}
	
	/**
	 * Add the input string before the current page title
	 *
	 * @param string $prependString
	 */
	static function prependPageTitle($prependString, $separator = " - ") {
		$inst = SysResponse::getInstance ();
		$inst->prependPageTitle ( $prependString.$separator );
	}
	
	
	/**
	 * Get all header information inside respond then print it.
	 * Header information include:
	 * 		+	Title
	 * 		+ 	Meta Data
	 * 		+	JavaScript
	 * 		+	StyleSheet
	 *
	 * @return string
	 */
	static function getHeaderHtml() {
		$inst = SysResponse::getInstance ();
		return $inst->getHeaderHtml ();
	}
	
	/**
	 * Echo all header information inside respond then print it.
	 * Header information include:
	 * 		+	Title
	 * 		+ 	Meta Data
	 * 		+	JavaScript
	 * 		+	StyleSheet
	 *
	 */
	static function printHeaderHtml() {
		$prepare = &SysResponse::_isHeaderPreparing ();
		$prepare = true;
		ob_start ( array ('SysResponse', '_printHeaderHtmlCallback' ) );
	}
	
	/**
	 * Print header then clear all information inside it
	 * for preparing reuse at the footer
	 *
	 * @param boolean $clearHeaderInfo clean the header information in SysResponse object or not? 
	 */
	static function printHeaderInfomation($clearHeaderInfo = true) {
		$inst = SysResponse::getInstance ();
		echo $inst->getHeaderHtml ( true );
		$inst->clearAllHeaderInfo ();
	}
	
	/**
	 * Clear all header information
	 * - metadata
	 * - javascript
	 * - stylesheet
	 *
	 */
	static function clearAllHeaderInfo() {
		$inst = SysResponse::getInstance ();
		$inst->clearAllHeaderInfo ();
	}
	
	/**
	 * Print the footer script information
	 *
	 */
	static function printFooterInfomation() {
		$inst = SysResponse::getInstance ();
		echo $inst->getHeaderHtml ( false );
	}
	
	/**
	 * Call back static function for print the HTML header.
	 *
	 * @param string $buffer
	 * @return string
	 */
	static function _printHeaderHtmlCallback($buffer) {
		$header = SysResponse::getHeaderHtml ();
		return "$header \n $buffer";
	}
	
	/**
	 * Get the application content of Respond
	 *
	 * @return string
	 */
	static function getAppContent() {
		$inst = SysResponse::getInstance ();
		$appContent = $inst->getAppContent ();
		
		if (! (defined ( '_FW_AJAX_SUPPORTED' ) && _FW_AJAX_SUPPORTED)) {
			return $appContent;
		}
		
		$sysResource = SysRequest::cloneResource();
		$sysResource->removeExtendWithoutPrefix();
		if (SystemSite::isBackEnd ()) {
			$link = $sysResource->getLink ( true, false );
			$content = '<form method="post" name="systemForm"  enctype="multipart/form-data" id="systemForm" action="' . $link . '">';
			$content .= $sysResource->createHtmlHiddenFields ( true, "_sys" );
			$content .= $appContent;
			$content .= '</form>';
		} else {
			$content = $appContent;
			$content .= $sysResource->createHtmlFormWithHidden ( "systemForm", false );
		}
		return $content;
	}
	
	/**
	 * Echo the application content of Respond
	 *
	 */
	static function printAppContent() {
		echo SysResponse::getAppContent ();
	}
	
	/**
	 * Print the module position.
	 * Position could be: left, right, top, footer,.....
	 *
	 * @param string $position Position could be: left, right, top, footer,.....
	 */
	static function printModulePosition($position) {
		if ($_GET ["showpositioninfo"]) {
			echo '<div style="background: #DDDDDD; border:1px dotted black; color:#770000; opacity:0.5;">';
			echo '<div style="position: absolute; z-index: 10000; background: #FFFFFF; border:1px solid black; color:red;">';
			echo $position;
			echo '</div>';
			echo SysTemplate::getMoldulePosition ( $position );
			echo '</div>';
			return;
		}
		echo SysTemplate::getMoldulePosition ( $position );
	}
	
	/**
	 * Set the respond header
	 *
	 * @param string $header
	 */
	static function setHeader($header) {
		header ( $header );
	}
	
	/**
	 * Enqueue a system message.
	 *
	 * @access	public
	 * @param	string 	$msg 	The message to enqueue.
	 * @param	string	$type	The message type.
	 * @return	void
	 */
	static function enqueueMessage($msg, $type = 'message') {
		$inst = SysResponse::getInstance ();
		return $inst->enqueueMessage ( $msg, $type );
	}
	
	/**
	 * Renders the error stack and returns the results as a string
	 *
	 * @access public
	 */
	static function getMessagesHtml() {
		$inst = SysResponse::getInstance ();
		
		// Initialize variables
		$contents = null;
		$lists = null;
		
		// Get the message queue
		$messages = $inst->getMessages ();
		
		// Build the sorted message list
		if (is_array ( $messages ) && count ( $messages )) {
			foreach ( $messages as $msg ) {
				if (isset ( $msg ['type'] ) && isset ( $msg ['message'] )) {
					$lists [$msg ['type']] [] = $msg ['message'];
				}
			}
		}
		
		// If messages exist render them
		if (is_array ( $lists )) {
			// Build the return string
			$contents .= "\n<dl id=\"system-message\">";
			foreach ( $lists as $type => $msgs ) {
				if (count ( $msgs )) {
					$contents .= "\n<dt class=\"" . strtolower ( $type ) . "\">" . SysLanguage::_ ( $type ) . "</dt>";
					$contents .= "\n<dd class=\"" . strtolower ( $type ) . " message fade\">";
					$contents .= "\n\t<ul>";
					foreach ( $msgs as $msg ) {
						$contents .= "\n\t\t<li>" . $msg . "</li>";
					}
					$contents .= "\n\t</ul>";
					$contents .= "\n</dd>";
				}
			}
			$contents .= "\n</dl>";
		}
		return $contents;
	}
	
	/**
	 * Renders and print the error stack
	 *
	 * @access public
	 * @param string 	$name		(unused)
	 * @param array 	$params		Associative array of values
	 * @return string	The output of the script
	 */
	static function printMessagesHtml() {
		echo SysResponse::getMessagesHtml ();
	}
	
	/**
	 * If there is some ob_start() for the header then
	 * we will print the header & the rest of site here.
	 *
	 */
	static function echoAllHtml() {
		if (SysResponse::_isHeaderPreparing ()) {
			ob_end_flush ();
		}
	}
	
	/**
	 * Check if the PrintHeader is call or not.
	 *
	 * @access private
	 * @return boolean
	 */
	static function &_isHeaderPreparing() {
		static $preparing;
		if ($preparing === null) {
			$preparing = false;
		}
		
		return $preparing;
	}
	
	/**
	 * Print directly module value
	 *
	 * @param string $modName
	 * @param array/object $params
	 */
	static function printModule($modName, $params = null, $workZone = null  ) {
		$module = &SystemSite::getModule($modName, $params, $workZone);
		if ($module->isView()) {
			$module->display();
		}
	}

	/**
	 * Get directly module html
	 *
	 * @param string $modName
	 * @param array/object $params
	 * @return html string
	 */
	static function getModuleHtmlContent($modName, $params = null, $workZone = null ) {
		$module = &SystemSite::getModule($modName, $params, $workZone);
		if ($module->isView()) {
			return $module->getContent();
		}
		
		return null;
	}
}
?>